package com.example.selfcontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.selfcontrol.config.Response;
import com.example.selfcontrol.dto.FollowerInfoDTO;
import com.example.selfcontrol.entity.Follower;
import com.example.selfcontrol.entity.User;
import com.example.selfcontrol.mapper.FollowerMapper;
import com.example.selfcontrol.mapper.UserMapper;
import com.example.selfcontrol.service.FollowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FollowerServiceImpl implements FollowerService {
    @Autowired
    private FollowerMapper followerMapper ;

    @Autowired
    private UserMapper userMapper ;


    @Override
    public Response addFollower(int followeeId, int followerId) {
        Follower relation = new Follower(followeeId,followerId) ;
        try {
            followerMapper.insert(relation) ;
        }catch (Exception e) {
            e.printStackTrace();
            return Response.fail() ;
        }
        return Response.ok() ;
    }

    // 是否关注
    @Override
    public boolean isFollower( int followeeId, int followerId) {
        QueryWrapper<Follower> wrapper = new QueryWrapper<>() ;
        wrapper.eq("followee_id",followeeId).eq("follower_id",followerId);
        Follower find = followerMapper.selectOne(wrapper) ;
        System.out.println(find);
        System.out.println("a: " + followeeId);
        System.out.println("a: " + followerId);
        if(find != null)    return true ;
        return false ;
    }

    // 返回关注列表
    @Override
    public List<FollowerInfoDTO> getFollowers(int userId , int pageNum, int pageSize) {
        Page<Follower> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Follower> wrapper = new QueryWrapper<>() ;
        wrapper.eq("follower_id",userId) ;
        List<Follower> followers = followerMapper.selectPage(page,wrapper).getRecords() ;

        List<FollowerInfoDTO> res  = new ArrayList<>();
        for(Follower follower : followers) {
            User user = userMapper.selectById(follower.getFolloweeId()) ;
            res.add(new FollowerInfoDTO(user.getId(), user.getName(),user.getPicture())) ;
        }
        //当前页的结果集
        return res ;
    }

    // 返回粉丝列表
    @Override
    public List<FollowerInfoDTO> getFollowees(int userId, int pageNum, int pageSize) {
        Page<Follower> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Follower> wrapper = new QueryWrapper<>() ;
        wrapper.eq("followee_id",userId) ;
        List<Follower> followees = followerMapper.selectPage(page,wrapper).getRecords() ;

        List<FollowerInfoDTO> res  = new ArrayList<>();
        for(Follower follower : followees) {
            User user = userMapper.selectById(follower.getFollowerId()) ;
            res.add(new FollowerInfoDTO(user.getId(), user.getName(),user.getPicture())) ;
        }
        //当前页的结果集
        return res ;
    }
}
